package cn.jinjimi.app.store;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabaseLockedException;

import cn.jinjimi.framework.helper.annotation.EventTrace;
import cn.jinjimi.app.AppContext;
import cn.jinjimi.app.bean.EventID;
import cn.jinjimi.app.store.dao.ArticleDao;
import cn.jinjimi.app.store.dao.CacheDao;
import cn.jinjimi.app.store.dao.ClueEntityDao;
import cn.jinjimi.app.store.dao.ContactInfoDao;
import cn.jinjimi.app.store.dao.CustomerDao;
import cn.jinjimi.app.store.dao.LastFollowDao;
import cn.jinjimi.app.store.dao.NoticeEntityDao;
import cn.jinjimi.app.store.dao.ZdbProductAllDao;
import cn.jinjimi.app.store.dao.ZdbProductComDao;

import java.lang.ref.WeakReference;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by Jinsen on 16/2/22.
 */
public class DatabaseManager {

    private AtomicInteger mOpenCounter = new AtomicInteger();

    private static DatabaseManager _instance;
    private SQLiteDatabase mDB;

    private static final String DBNAME = "xrk_bxr.db";

    private ArticleDao mArticleDao;
    private CacheDao mCacheDao;
    private CustomerDao mCustomerDao;
    private ZdbProductAllDao mZdbProductAllDao;
    private ZdbProductComDao mZdbProductComDao;
    private NoticeEntityDao mNoticeEntityDao;
    private ClueEntityDao mClueEntityDao;
    private ContactInfoDao mContactInfoDao;
    private LastFollowDao mLastFollowDao;

    private static final int VERSION = 10;

    private DatabaseManager(final Context context) {
        try {
            OpenHelper devOpenHelper = new OpenHelper(context, DBNAME, null, VERSION);
            mDB = devOpenHelper.getReadableDatabase();

            DaoMaster mDaoMaster = new DaoMaster(mDB, VERSION);
            DaoSession mDaoSession = (DaoSession) mDaoMaster.newSession();

            mArticleDao = mDaoSession.getArticleDao();
            mCacheDao = mDaoSession.getCacheDao();
            mCustomerDao = mDaoSession.getCustomerDao();
            mZdbProductAllDao = mDaoSession.getZdbProductAllDao();
            mZdbProductComDao = mDaoSession.getZdbProductComDao();
            mNoticeEntityDao = mDaoSession.getNoticeEntityDao();
            mClueEntityDao = mDaoSession.getClueEntityDao();
            mContactInfoDao = mDaoSession.getContactInfoDao();
            mLastFollowDao = mDaoSession.getLastFollowDao();
        } catch (SQLiteDatabaseLockedException e) {
            analyseError();
        }
    }

    @EventTrace(EventID.INIT_DATEBASE_ERROR)
    private void analyseError() {
    }

    private static WeakReference<Context> mContextRef;

    public static void init(Context context) {
        mContextRef = new WeakReference<>(context);
    }

    public static Context getContext() {
        Context context = mContextRef != null ? mContextRef.get() : null;
        return context != null ? context : AppContext.getInstance();
    }

    public static DatabaseManager getInstance() {
        if (_instance == null || _instance.mDB == null) {
            _instance = new DatabaseManager(getContext());
        }
        return _instance;
    }

    public ArticleDao getArticleDao() {
        return mArticleDao;
    }

    public CustomerDao getCustomerDao() {
        return mCustomerDao;
    }

    public CacheDao getCacheDao() {
        return mCacheDao;
    }

    public ZdbProductAllDao getZdbProductAllDao() {
        return mZdbProductAllDao;
    }

    public ZdbProductComDao getZdbProductComDao() {
        return mZdbProductComDao;
    }

    public NoticeEntityDao getNoticeEntityDao() {
        return mNoticeEntityDao;
    }

    public ClueEntityDao getClueEntityDao() {
        return mClueEntityDao;
    }

    public ContactInfoDao getContactInfoDao() {
        return mContactInfoDao;
    }

    public LastFollowDao getLastFollowDao() {
        return mLastFollowDao;
    }

    public void onTerminal() {
        if (mDB != null && mDB.isOpen()) {
            if (mDB.inTransaction())
                mDB.endTransaction();
            mDB.close();
            mDB = null;
            _instance = null;
        }
    }
}
